<template>
  <div>
    <div class="header-Main" style="margin-top: 10px; margin-left: 5px;"> 
      <el-input v-model="name" placeholder="输入内容" suffix-icon="el-icon-search" style="width: 500px;"></el-input>
      <el-button type="primary" style="margin: 0 20px 0 5px;" @click="handlget">查询</el-button>
      <el-button type="primary">
        <div @click="add" style="color: aliceblue;">增加<i class="el-icon-circle-plus-outline iconadd"
          style="font-size: 15px;"></i></div>
        </el-button>
    </div>

    <div v-if="tableData != null && tableData != ''">
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark"
        border style="width: 100%">

        <el-table-column prop="id" label="ID" width="100">

        </el-table-column>

        <el-table-column prop="image" label="图片" width="160">
          <template slot-scope="scope">
            <div class="imagecontainer">
              <img :src="scope.row.image" style="height: 100%; width: 100%;">
            </div>
            <div class="play" @click="musicplay(scope.row.url,scope.row.id)">
              <span class="iconfont icon-play" v-if="toggle !== scope.row.id"></span>
              <span class="iconfont icon-bofang" v-else></span>
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="name" label="歌曲名字" width="140" height="50">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>

        <el-table-column prop="album" label="歌曲专辑" width="140">
          <template slot-scope="scope">{{ scope.row.album}}</template>
        </el-table-column>


        <el-table-column prop="date" label="创建时间" width="160">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>

        <el-table-column prop="date" label="更新时间" width="160">
          <template slot-scope="scope">{{ scope.row.updateTime}}</template>
        </el-table-column>

        <el-table-column prop="lyric" label="歌词">
          <template slot-scope="scope">
            <ul style="height: 120px;overflow-y: scroll;">
              <li v-for="(item,index) in parselyric(scope.row.lyric)" :key="index">
                {{ item }}
              </li>
            </ul>
          </template>
        </el-table-column>

        <el-table-column width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit"  circle @click="handlEditor(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="handlDelete(scope.row.id)"></el-button>
          </template>
        </el-table-column>

      </el-table>

      <div class="pageion"> 
        <pagination :total="total" :PageSize="PageSize" :PageNum="PageNum" 
            @ChangeSize="changesize" @ChangeNum="changenum"></pagination>
        </div>
    </div>

    <div v-else>

    </div>
  </div>
</template>

<script>
import pagination from '@/components/Pagination/index.vue'
import { mapState,mapMutations} from 'vuex'
export default {
  components:{
    pagination,
  },
  data() {
      return {
        singerId:'',
        name:'',
        id: '',
        tableData: [],
        total:'',
        PageSize:4,
        PageNum:1,
        toggle:''
      }
    },
  methods:{
    ...mapMutations('SongDatas',['changeTitle','changeDatas']),
    ...mapMutations('music',['changeIsplay','getUrl']),
    changesize(val){
      this.PageSize=val
      this.getAll()
    },
    changenum(val){
      this.PageNum=val
      this.getAll()
    },
    add(){
      this.changeTitle("添加")
      this.$router.push("/addsong")
    },
    async getAll(){
      if(this.SingerId != ''){
        console.log(this.SingerId)
        const res = await this.$axios.post(`Song/SelectAll?PageNum=${this.PageNum}&PageSize=${this.PageSize}&singerId=${this.SingerId}&name=${this.name}`)
        console.log(res)
        if(res != null){
          this.total = res.total
          console.log("[p[poppoooooooaua]]")
          console.log(res.total)
          this.tableData = res.data
        }
      }
    },
    async handlget(){
      await this.getAll()
    },
    async handlDelete(val){
      const res = await this.$axios.delete(`Song/DeleteSong/${val}`)
      console.log(res)
      this.$message.success(res.msg)
      this.getAll()
    },
    async handlEditor(val){
      const res = await this.$axios.get(`Song/SelectBySongId/${val}`)
      console.log(res)
      this.changeTitle("编辑")
      this.changeDatas(res)
      this.$router.push("/AddSong")
    },

    parselyric(text){
      let line = text.split("\n")
      let parren = /\[\d{2}:\d{2}.(\d{3}|\d{2})\]/g
      let resut = []
      for(let item of line){
        let value = item.replace(parren,'')
        resut.push(value)
      }
        return resut
    },
    musicplay(url,id){
      if(this.toggle == id){
        this.toggle =-1
        this.changeIsplay(false)
      }
      else{
        this.toggle = id
        this.changeIsplay(true)
        this.getUrl(url)
      }
    }
  },
  computed:{
    ...mapState('song1',['SingerId']),
    ...mapState('music',['url','isPlay'])
  },
  created(){
    this.getAll()
  }
}
</script>

<style scoped lang="less">
.header-Main{
  margin-bottom: 10px;
}

.iconadd{
  display: inline-block;
  font-size: 5px;
  padding-left: 5px;
}

.pageion{
  display: flex;
  margin-top: 30px;
  justify-content: center;
}

::v-deep .el-table--scrollable-x .el-table__body-wrapper {
    height: 150px;
    overflow-y: scroll;

  }
  ::v-deep .el-table th{
    text-align: center;
  }
  ::v-deep .el-table td, .el-table th {
    text-align: center;
  }
  .imagecontainer{
    height: 140px;
    width: 140px;
  }

.play{
  position: absolute;
  z-index: 99;
  left: 50%;
  bottom: 50%;
  transform: translate(-50%,50%);
}
.play span{
  font-size: 50px;
}

.play span:hover{
  background-color: #b3b0b0;
}
</style>